package case1;

import case1.adapter.GooseDuckAdapter;
import case1.composite.Flock;
import case1.decorator.QuackCounter;
import case1.factory.AbstractDuckFactory;
import case1.factory.CountingDuckFactory;
import case1.factory.DuckFactory;
import case1.goose.Goose;
import case1.core.observer.Quackologist;

/**
 *
 * @author yuanml
 */
public class DuckSimulator {
    public static void main(String[] args) {
        AbstractDuckFactory duckCountingFactory = new CountingDuckFactory();
        AbstractDuckFactory duckFactory = new DuckFactory();
        DuckSimulator duckSimulator = new DuckSimulator();
//        duckSimulator.simulate1(duckCountingFactory);
        duckSimulator.simulate2(duckFactory);
    }

    private void simulate2(AbstractDuckFactory duckFactory) {
        Quackologist quackologist1 = new Quackologist();
        Quackologist quackologist2 = new Quackologist();
        Quackologist quackologist3 = new Quackologist();
        Quackologist quackologist4 = new Quackologist();
        Quackable mallardDuck = duckFactory.createMallardDuck();
        Quackable redheadDuck1 = duckFactory.createRedheadDuck();
        redheadDuck1.registerObserver(quackologist1);
        redheadDuck1.registerObserver(quackologist4);
        mallardDuck.registerObserver(quackologist1);
        mallardDuck.registerObserver(quackologist2);
        mallardDuck.registerObserver(quackologist3);
        mallardDuck.registerObserver(quackologist4);
        simulate(mallardDuck);
        simulate(redheadDuck1);
    }

    void simulate1(AbstractDuckFactory factory) {
        Quackable mallardDuck0 = factory.createMallardDuck();
        Quackable mallardDuck1= factory.createMallardDuck();
        Quackable mallardDuck2 = factory.createMallardDuck();
        Quackable redheadDuck0 = factory.createRedheadDuck();
        Quackable redheadDuck1 = factory.createRedheadDuck();
        Quackable redheadDuck2 = factory.createRedheadDuck();
        Quackable duckCall0 = factory.createDuckCall();
        Quackable duckCall1 = factory.createDuckCall();
        Quackable duckCall2 = factory.createDuckCall();
        Quackable duckCall3 = factory.createDuckCall();
        Quackable duckCall4 = factory.createDuckCall();
        Quackable rubberDuck0 = factory.createRubberDuck();
        Quackable rubberDuck1 = factory.createRubberDuck();
        Quackable goose0 = new GooseDuckAdapter(new Goose());
        Quackable goose1 = new GooseDuckAdapter(new Goose());
        Quackable goose2 = new GooseDuckAdapter(new Goose());
        Quackable goose3 = new GooseDuckAdapter(new Goose());
        System.out.println("Duck simulator");
        Flock flock = new Flock();
        flock.add(mallardDuck0);
        flock.add(mallardDuck1);
        flock.add(mallardDuck2);
        flock.add(redheadDuck0);
        flock.add(mallardDuck1);
        flock.add(mallardDuck2);
        flock.add(redheadDuck1);
        flock.add(redheadDuck2);
        flock.add(duckCall0);
        flock.add(duckCall1);
        flock.add(duckCall2);
        flock.add(duckCall3);
        flock.add(duckCall4);
        flock.add(rubberDuck0);
        flock.add(rubberDuck1);
        flock.add(goose0);
        flock.add(goose1);
        flock.add(goose2);
        flock.add(goose3);
        Quackologist quackologist = new Quackologist();
        flock.registerObserver(quackologist);
        mallardDuck0.registerObserver(quackologist);
        simulate(flock);
        System.out.println("count = " + QuackCounter.count);
    }

    void simulate(Quackable duck) {
        duck.quack();
    }
}
